<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('销假申请') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input
          v-model="queryParams.actTitle"
          size="small"
          :placeholder="$t('申请标题')"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('申请人')" prop="applicant">
        <userSelect
          v-model="queryParams.applicant"
          size="small"
          :placeholder="$t('申请人')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('申请时间')" prop="applicationTime">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:leaveOff:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="oaLeaveOffList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请标题')" align="center" prop="actTitle" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请人')" align="center" prop="applicantName" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('休假类型')" align="center" prop="" show-overflow-tooltip width="200">
        <template slot-scope="scope">
          <span
            v-for="(item,index) in scope.row.oaVacationDetails"
            :key="index"
          >
            {{ $t(item.type) }}{{ index === scope.row.oaVacationDetails.length - 1 ? '' : ',' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请时间')" align="center" prop="applicationTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applicationTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('起始日期')" align="center" prop="startDate" show-overflow-tooltip width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate) }} / {{ parseTime(scope.row.endDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请休假天数')" align="center" prop="removeHoliday" show-overflow-tooltip width="100" />
      <el-table-column :label="$t('审批状态')" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
          <ListBtn :bus-data="scope.row" has-permi="oa:leaveOff" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销假申请对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <oaLeaveOffForm ref="oaLeaveOffForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listOaLeaveOff, delOaLeaveOff } from '@/api/oa/leave/oaLeaveOff'
import oaLeaveOffForm from './oaLeaveOffForm.vue'

export default {
  components: {
    oaLeaveOffForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 销假申请表格数据
      oaLeaveOffList: [],
      // 弹出层标题
      title: '销假申请',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        applicant: undefined,
        applicationTime: undefined,
        actTitle: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dateRange: [],
      otherBtnHide: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询销假申请列表 */
    getList() {
      this.loading = true
      listOaLeaveOff(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.oaLeaveOffList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        applicant: undefined,
        applicationTime: undefined,
        deptId: undefined,
        phone: undefined,
        startDate: undefined,
        endDate: undefined,
        vacationDay: undefined,
        removeHoliday: undefined,
        workUser: undefined,
        noticeUsers: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        actId: undefined,
        actStatus: '0',
        actTitle: undefined,
        vacationId: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = undefined
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.oaLeaveOffForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.oaLeaveOffForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delOaLeaveOff(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    }
  }
}
</script>
